package com.wip.reportingclient.services.impls;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.wip.reportingclient.services.domains.ReportData;
import com.wip.reportingclient.services.interfaces.IPdfService;
import com.wip.reportingclient.services.interfaces.IWriterService;
import com.wip.reportingclient.services.interfaces.IZippingService;
import com.wip.reportingclient.services.util.CalculatorUtil;
import com.wip.reportingclient.ui.components.FormData;

@Service
public class PdfServiceImpl implements IPdfService {

	@Autowired
	private CalculatorUtil calcs;

	@Autowired
	private IZippingService zipService;

	@Autowired
	private IWriterService writeService;

	@Override
	public void createReport(HttpServletResponse response, FormData formData,
			ReportData reportData) {
		HashMap<String, HashMap<String, HashMap<String, ArrayList<Integer>>>> clientsMap = reportData
				.getDataMap();

		Map<String, ByteArrayOutputStream> bosMap = new HashMap<String, ByteArrayOutputStream>();

		for (String client : clientsMap.keySet()) {

			Map<String, String[]> map = new TreeMap<String, String[]>();

			int numOfDates = clientsMap.get(client).values().size();
			int cols = numOfDates + 1;

			HashMap<String, HashMap<String, ArrayList<Integer>>> logsMap = clientsMap
					.get(client);

			String[] header = new String[cols];
			int h = 1;
			header[0] = " Pages";
			for (String log : logsMap.keySet()) {
				header[h] = log;
				h++;
			}

			// map.put(" Pages", header);
			map.put(" Pages", header);

			int logIndex = 1;
			for (String log : logsMap.keySet()) {

				HashMap<String, ArrayList<Integer>> pagesMap = logsMap.get(log);

				// int row = 0;
				for (String page : pagesMap.keySet()) {
					String[] data = new String[cols];

					List<Integer> timesList = pagesMap.get(page);

					if (map.containsKey(page)) {
						data = map.get(page);
					} else {
						data[0] = page;
					}

					data[logIndex] = String.valueOf(timesList.get(0));

					map.put(page, data);
				}
				logIndex++;
			}

			try {

				// Get the text that will be added to the PDF
				String text = formData.getReport();
				Paragraph paragraph = new Paragraph(text);
				paragraph.setAlignment(Element.ALIGN_CENTER);

				// step 1
				Document document = new Document();
				// step 2
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				PdfWriter.getInstance(document, baos);
				// step 3
				document.open();
				// step 4
				document.add(paragraph);
				document.add(new Paragraph(" ")); // blank line to space it out

				PdfPTable table = new PdfPTable(cols);

				for (String[] vals : map.values()) {
					for (int c = 0; c < cols; c++) {
						table.addCell(vals[c]);
					}
				}

				document.add(table);

				// step 5
				document.close();

				bosMap.put(client + ".pdf", baos);

			} catch (DocumentException e) {
				e.printStackTrace();
				// log.error("error: "+e);
			} catch (Exception ex) {
				// log.debug("error: " + ex.getMessage());
				ex.printStackTrace();
			}
		}

		if (bosMap.size() == 1) {
			// this will loop once only
			for (String fileName : bosMap.keySet()) {
				writeService.writePdf(response, bosMap.get(fileName), fileName);

			}
		} else {
			zipService.writeZip(response, bosMap);

		}

	}

}
